home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / SQL Format252808242001.psc / MOD_RESIZE.bas < prev    next >
Encoding:
BASIC Source File  |  2001-05-03  |  10.1 KB  |  303 lines

  1. Attribute VB_Name = "MOD_RESIZE"
  2. 'Workfile:      RS_FORM.BAS
  3. 'Created:       06/18/98
  4. 'Updated:       06/18/98
  5. 'Author:        Scott Whitlow
  6. 'Description:   This module provides the code needed to
  7. '               adjust the placement of all controls on
  8. '               a form. There are three public subs.
  9. '               How to use this module:
  10. '                   In a forms Resize event type
  11. '                       ResizeForm Me
  12. '                           - This will resize all controls
  13. '                             on the form to match new form size
  14. '                   You can save a default form size by calling
  15. '                       SaveFormPosition Me
  16. '                   You can restore a form to its original size or
  17. '                   the size that was stored using the StoreFormPosition
  18. '                   sub by calling
  19. '                       RestoreFormPosition Me
  20. 'Dependencies:  None
  21. 'Issues:        MDIChild forms caused a memory stack overflow error
  22. '               Resolved: Code was changed to be more MDIChild aware
  23. '                   Note: Do not make MDIChild Forms Maximized at design time.
  24. '                         You may change the WindowState property after the
  25. '                         Resize event has occured once durring runtime.
  26. '                   Please E-Mail problems to swhitlow@finishlines.com
  27. Option Explicit
  28.  
  29. Public Type ctrObj
  30.     Name As String
  31.     Index As Long
  32.     Parrent As String
  33.     Top As Long
  34.     Left As Long
  35.     Height As Long
  36.     Width As Long
  37.     ScaleHeight As Long
  38.     ScaleWidth As Long
  39. End Type
  40.  
  41. Private FormRecord() As ctrObj
  42. Private ControlRecord() As ctrObj
  43. Private bRunning As Boolean
  44. Private MaxForm As Long
  45. Private MaxControl As Long
  46.  
  47. Private Function ActualPos(plLeft As Long) As Long
  48.     If plLeft < 0 Then
  49.         ActualPos = plLeft + 75000
  50.     Else
  51.         ActualPos = plLeft
  52.     End If
  53. End Function
  54. Public Sub Win95Shrivel(frm As Form)
  55. frm.WindowState = vbMinimized
  56. End Sub
  57. Private Function FindForm(pfrmIn As Form) As Long
  58. Dim i As Long
  59.     FindForm = -1
  60.     If MaxForm > 0 Then
  61.         For i = 0 To (MaxForm - 1)
  62.             If FormRecord(i).Name = pfrmIn.Name Then
  63.                 FindForm = i
  64.                 Exit Function
  65.             End If
  66.         Next i
  67.     End If
  68. End Function
  69.  
  70. Private Function AddForm(pfrmIn As Form) As Long
  71. Dim FormControl As Control
  72. Dim i As Long
  73.     ReDim Preserve FormRecord(MaxForm + 1)
  74.     FormRecord(MaxForm).Name = pfrmIn.Name
  75.     FormRecord(MaxForm).Top = pfrmIn.Top
  76.     FormRecord(MaxForm).Left = pfrmIn.Left
  77.     FormRecord(MaxForm).Height = pfrmIn.Height
  78.     FormRecord(MaxForm).Width = pfrmIn.Width
  79.     FormRecord(MaxForm).ScaleHeight = pfrmIn.ScaleHeight
  80.     FormRecord(MaxForm).ScaleWidth = pfrmIn.ScaleWidth
  81.     AddForm = MaxForm
  82.     MaxForm = MaxForm + 1
  83.     For Each FormControl In pfrmIn
  84.         i = FindControl(FormControl, pfrmIn.Name)
  85.         If i < 0 Then
  86.             i = AddControl(FormControl, pfrmIn.Name)
  87.         End If
  88.     Next FormControl
  89. End Function
  90.  
  91. Private Function FindControl(inControl As Control, inName As String) As Long
  92. Dim i As Long
  93.     FindControl = -1
  94.     For i = 0 To (MaxControl - 1)
  95.         If ControlRecord(i).Parrent = inName Then
  96.             If ControlRecord(i).Name = inControl.Name Then
  97.                 On Error Resume Next
  98.                 If ControlRecord(i).Index = inControl.Index Then
  99.                     FindControl = i
  100.                     Exit Function
  101.                 End If
  102.                 On Error GoTo 0
  103.             End If
  104.         End If
  105.     Next i
  106. End Function
  107.  
  108. Private Function AddControl(inControl As Control, inName As String) As Long
  109.     ReDim Preserve ControlRecord(MaxControl + 1)
  110.     On Error Resume Next
  111.     ControlRecord(MaxControl).Name = inControl.Name
  112.     ControlRecord(MaxControl).Index = inControl.Index
  113.     ControlRecord(MaxControl).Parrent = inName
  114.     If TypeOf inControl Is Line Then
  115.         ControlRecord(MaxControl).Top = inControl.Y1
  116.         ControlRecord(MaxControl).Left = ActualPos(inControl.X1)
  117.         ControlRecord(MaxControl).Height = inControl.Y2
  118.         ControlRecord(MaxControl).Width = ActualPos(inControl.X2)
  119.     Else
  120.         ControlRecord(MaxControl).Top = inControl.Top
  121.         ControlRecord(MaxControl).Left = ActualPos(inControl.Left)
  122.         ControlRecord(MaxControl).Height = inControl.Height
  123.         ControlRecord(MaxControl).Width = inControl.Width
  124.     End If
  125.     inControl.IntegralHeight = False
  126.     On Error GoTo 0
  127.     AddControl = MaxControl
  128.     MaxControl = MaxControl + 1
  129.     
  130. ExitHere:
  131. Exit Function
  132.  
  133. ErrorTrap:
  134.   GoTo ExitHere
  135.  
  136. End Function
  137.  
  138. Private Function PerWidth(pfrmIn As Form) As Long
  139. Dim i As Long
  140.     i = FindForm(pfrmIn)
  141.     If i < 0 Then
  142.         i = AddForm(pfrmIn)
  143.     End If
  144.     PerWidth = (pfrmIn.ScaleWidth * 100) \ FormRecord(i).ScaleWidth
  145. End Function
  146.  
  147. Private Function PerHeight(pfrmIn As Form) As Single
  148. Dim i As Long
  149.     i = FindForm(pfrmIn)
  150.     If i < 0 Then
  151.         i = AddForm(pfrmIn)
  152.     End If
  153.     PerHeight = (pfrmIn.ScaleHeight * 100) \ FormRecord(i).ScaleHeight
  154. End Function
  155.  
  156. Private Sub ResizeControl(inControl As Control, pfrmIn As Form)
  157. On Error Resume Next
  158. Dim i As Long
  159. Dim widthfactor As Single, heightfactor As Single
  160. Dim minFactor As Single
  161. Dim yRatio, xRatio, lTop, lLeft, lWidth, lHeight As Long
  162.     If Left(inControl.Tag, 8) = "Resize=N" Then
  163.         Exit Sub
  164.     End If
  165.     yRatio = PerHeight(pfrmIn)
  166.     xRatio = PerWidth(pfrmIn)
  167.     i = FindControl(inControl, pfrmIn.Name)
  168.     If inControl.Left < 0 Then
  169.         lLeft = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000)
  170.     Else
  171.         lLeft = CLng((ControlRecord(i).Left * xRatio) \ 100)
  172.     End If
  173.     lTop = CLng((ControlRecord(i).Top * yRatio) \ 100)
  174.     lWidth = CLng((ControlRecord(i).Width * xRatio) \ 100)
  175.     lHeight = CLng((ControlRecord(i).Height * yRatio) \ 100)
  176.     If TypeOf inControl Is Line Then
  177.         If inControl.X1 < 0 Then
  178.             inControl.X1 = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000)
  179.         Else
  180.             inControl.X1 = CLng((ControlRecord(i).Left * xRatio) \ 100)
  181.         End If
  182.         inControl.Y1 = CLng((ControlRecord(i).Top * yRatio) \ 100)
  183.         If inControl.X2 < 0 Then
  184.             inControl.X2 = CLng(((ControlRecord(i).Width * xRatio) \ 100) - 75000)
  185.         Else
  186.             inControl.X2 = CLng((ControlRecord(i).Width * xRatio) \ 100)
  187.         End If
  188.         inControl.Y2 = CLng((ControlRecord(i).Height * yRatio) \ 100)
  189.     Else
  190.         inControl.Move lLeft, lTop, lWidth, lHeight
  191.         inControl.Move lLeft, lTop, lWidth
  192.         inControl.Move lLeft, lTop
  193.     End If
  194. End Sub
  195.  
  196. Public Sub ResizeForm(pfrmIn As Form)
  197. Dim FormControl As Control
  198. Dim isVisible As Boolean
  199. Dim StartX, StartY, MaxX, MaxY As Long
  200. Dim bNew As Boolean
  201. If Not bRunning Then
  202.     bRunning = True
  203.     If FindForm(pfrmIn) < 0 Then
  204.         bNew = True
  205.     Else
  206.         bNew = False
  207.     End If
  208.     If pfrmIn.Top < 30000 Then
  209.         isVisible = pfrmIn.Visible
  210.         On Error Resume Next
  211.         If Not pfrmIn.MDIChild Then
  212.             On Error GoTo 0
  213.            ' pfrmIn.Visible = False
  214.         Else
  215.             If bNew Then
  216.                 StartY = pfrmIn.Height
  217.                 StartX = pfrmIn.Width
  218.                 On Error Resume Next
  219.                 For Each FormControl In pfrmIn
  220.                     If FormControl.Left + FormControl.Width + 200 > MaxX Then
  221.                         MaxX = FormControl.Left + FormControl.Width + 200
  222.                     End If
  223.                     If FormControl.Top + FormControl.Height + 500 > MaxY Then
  224.                         MaxY = FormControl.Top + FormControl.Height + 500
  225.                     End If
  226.                     If FormControl.X1 + 200 > MaxX Then
  227.                         MaxX = FormControl.X1 + 200
  228.                     End If
  229.                     If FormControl.Y1 + 500 > MaxY Then
  230.                         MaxY = FormControl.Y1 + 500
  231.                     End If
  232.                     If FormControl.X2 + 200 > MaxX Then
  233.                         MaxX = FormControl.X2 + 200
  234.                     End If
  235.                     If FormControl.Y2 + 500 > MaxY Then
  236.                         MaxY = FormControl.Y2 + 500
  237.                     End If
  238.                 Next FormControl
  239.                 On Error GoTo 0
  240.                 pfrmIn.Height = MaxY
  241.                 pfrmIn.Width = MaxX
  242.             End If
  243.             On Error GoTo 0
  244.         End If
  245.         For Each FormControl In pfrmIn
  246.             ResizeControl FormControl, pfrmIn
  247.         Next FormControl
  248.         On Error Resume Next
  249.         If Not pfrmIn.MDIChild Then
  250.             On Error GoTo 0
  251.             pfrmIn.Visible = isVisible
  252.         Else
  253.             If bNew Then
  254.                 pfrmIn.Height = StartY
  255.                 pfrmIn.Width = StartX
  256.                 For Each FormControl In pfrmIn
  257.                     ResizeControl FormControl, pfrmIn
  258.                 Next FormControl
  259.             End If
  260.         End If
  261.         On Error GoTo 0
  262.     End If
  263.     bRunning = False
  264. End If
  265. End Sub
  266.  
  267. Public Sub SaveFormPosition(pfrmIn As Form)
  268. Dim i As Long
  269.     If MaxForm > 0 Then
  270.         For i = 0 To (MaxForm - 1)
  271.             If FormRecord(i).Name = pfrmIn.Name Then
  272.                 FormRecord(i).Top = pfrmIn.Top
  273.                 FormRecord(i).Left = pfrmIn.Left
  274.                 FormRecord(i).Height = pfrmIn.Height
  275.                 FormRecord(i).Width = pfrmIn.Width
  276.                 Exit Sub
  277.             End If
  278.         Next i
  279.         AddForm (pfrmIn)
  280.     End If
  281. End Sub
  282.  
  283. Public Sub RestoreFormPosition(pfrmIn As Form)
  284. Dim i As Long
  285.     If MaxForm > 0 Then
  286.         For i = 0 To (MaxForm - 1)
  287.             If FormRecord(i).Name = pfrmIn.Name Then
  288.                 If FormRecord(i).Top < 0 Then
  289.                     pfrmIn.WindowState = 2
  290.                 ElseIf FormRecord(i).Top < 30000 Then
  291.                     pfrmIn.WindowState = 0
  292.                     pfrmIn.Move FormRecord(i).Left, FormRecord(i).Top, FormRecord(i).Width, FormRecord(i).Height
  293.                 Else
  294.                     pfrmIn.WindowState = 1
  295.                 End If
  296.                 Exit Sub
  297.             End If
  298.         Next i
  299.     End If
  300. End Sub
  301.  
  302.  
  303.